package site.jlopen.controller.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import site.jlopen.entity.BaseResponse;
import site.jlopen.entity.vo.LoginRes;
import site.jlopen.service.intf.user.UserService;
import site.jlopen.utils.jwt.JwtTool;

@RestController
@RequestMapping(value="/api")
public class ApiAuthController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired private UserService userService;
	
	@RequestMapping(value = "/auth", method = RequestMethod.POST )
	@ResponseBody
	public BaseResponse<String> sublogin(
			@RequestParam(name = "username", required = false) String username,
			@RequestParam(name = "password", required = false) String password) {
		logger.info("api授权登陆：{}", username);
		BaseResponse<LoginRes> res = userService.login(username, password);
		if(res.getStatus() == 0) {
			String token = JwtTool.createToken(res.getData().getId(), res.getData().getName());
			return new BaseResponse<String>().success(res.getMsg()).setData(token);
		}
		return new BaseResponse<String>().error(res.getMsg());
		//Subject subject = SecurityUtils.getSubject();
		//subject.login(new JwtToken(username, password, false));
		//return new BaseResponse<String>().error("待整合");
	}
}